package cn.pangolin.pro.module.system.controller.admin.captcha;

import static cn.pangolin.pro.framework.common.pojo.CommonResult.success;

import cn.hutool.core.util.StrUtil;
import cn.pangolin.pro.framework.common.pojo.CommonResult;
import cn.pangolin.pro.framework.common.util.servlet.ServletUtils;
import cn.pangolin.pro.framework.operatelog.core.annotations.OperateLog;
import cn.pangolin.pro.module.system.controller.admin.captcha.vo.CaptchaRespVo;
import com.xingyuv.captcha.model.common.CaptchaTypeEnum;
import com.xingyuv.captcha.model.common.ResponseModel;
import com.xingyuv.captcha.model.vo.CaptchaVO;
import com.xingyuv.captcha.service.CaptchaCacheService;
import com.xingyuv.captcha.service.CaptchaService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 验证码
 *
 * @author Rex
 * @since 2025/6/24 14:39
 */
@Tag(name = "管理后台 - 验证码")
@RestController("adminCaptchaController")
@RequestMapping("/system/captcha")
@Slf4j
public class CaptchaController {

    @Resource
    private CaptchaService captchaService;
    @Resource
    private CaptchaCacheService captchaCacheService;

    @PostMapping({"/get"})
    @Operation(summary = "获得验证码")
    @PermitAll
    @OperateLog(enable = false) // 避免 Post 请求被记录操作日志
    public ResponseModel get(@RequestBody CaptchaVO data, HttpServletRequest request) throws IOException {
        assert request.getRemoteHost() != null;
        data.setBrowserInfo(getRemoteId(request));
        return captchaService.get(data);
    }

    @PostMapping({"/get-picture-word"})
    @Operation(summary = "获得验证码 v2，单纯的只要图片验证码")
    @PermitAll
    @OperateLog(enable = false) // 避免 Post 请求被记录操作日志
    public CommonResult<CaptchaRespVo> getPictureWord() {
        // 生成验证码
        CaptchaVO data = new CaptchaVO();
        data.setCaptchaType(CaptchaTypeEnum.PICTURE_WORD.getCodeValue());
        CaptchaVO captchaVO = (CaptchaVO) captchaService.get(data).getRepData();
        log.info("验证码: {}", captchaVO.getCaptchaVerification());
        CaptchaRespVo captchaRespVo = new CaptchaRespVo();
        captchaRespVo.setVerificationCodeId(captchaVO.getToken());
        captchaRespVo.setVerificationCodeUrl(captchaVO.getOriginalImageBase64());
        return success(captchaRespVo);
    }

    @PostMapping("/check")
    @Operation(summary = "校验验证码")
    @PermitAll
    @OperateLog(enable = false) // 避免 Post 请求被记录操作日志
    public ResponseModel check(@RequestBody CaptchaVO data, HttpServletRequest request) {
        data.setBrowserInfo(getRemoteId(request));
        return captchaService.check(data);
    }

    public static String getRemoteId(HttpServletRequest request) {
        String ip = ServletUtils.getClientIP(request);
        String ua = request.getHeader("user-agent");
        if (StrUtil.isNotBlank(ip)) {
            return ip + ua;
        }
        return request.getRemoteAddr() + ua;
    }

}
